import ajax from 'uni-ajax'
import parser from 'fast-xml-parser'
import db from './database'
import store from '@/store/index.js'
import ToolV1 from '@/utils/toolv1.js'

const http = { 
	// 获取视频源详情
	async getSite(key) {
		const site = store.state.defcku.sites.find(t => t.key == key)
		if (site) {
			return site
		}
		return false
	},
	// 获取视频源的分类
	async class(site) {
		let list = new Array()
		const res = await ajax.post(site.api)
		if (ToolV1.isXml(res.data)) {
			return ToolV1.videoClass(res)
		}
		if (res.data.code !== 1) {
			throw new Error(JSON.stringify(res.data))
		}
		for (const i of res.data.class) {
			const j = {
				tid: i.type_id,
				name: i.type_name
			}
			list.push(j)
		}
		// console.log(res.data.class)
		return list
	},
	// 获取视频资源
	async list(site, pg = 1, t) {
		const url = `${site.api}?ac=videolist${t ? '&t=' + t: ''}&pg=${pg}`
		try {
			// console.log(url)
			const res = await ajax.post(url)
			// console.log(typeof(res.data))
			if (ToolV1.isXml(res.data)) {
				let video = ToolV1.videoList(res)
				return video.videos
			}
			let data = this.handleResponse(res)
			// console.log(data)
			return data.videos ?? []
		} catch (err) {
			return err
		}
	},
	// 获取总资源数, 以及页数
	async page(site, t) {
		const url = `${site.api}?ac=videolist${t ? '&t=' + t: ''}`

		try {
			const res = await ajax.post(url)
			if (ToolV1.isXml(res.data)) {
				let video = ToolV1.videoList(res)
				return video
			}
			let data = this.handleResponse(res)
			// console.log(data)
			return data
		} catch (err) {
			return err
		}
	},
	handleResponse(res) {
		let page = 0
		let pagecount = 0
		let pagesize = 0
		let recordcount = 0
		let videos = new Array()
		if (res.data && typeof(res.data) == 'object') {
			console.log(res)
			pagecount = res.data.pagecount
			pagesize = res.data.limit
			recordcount = res.data.total
			page = res.data.page
			videos = res.data.list
			return { pagecount, pagesize, recordcount, page, videos }
		}
		throw new Error('数据解析失败' + res.data)
	},
	// 搜索资源
	async search(site, wd) {
		wd = encodeURI(wd)
		const url = `${site.api}?wd=${wd}`
		try {
			// console.log(url)
			const res = await ajax.post(url, { timeourt: 3000 })
			if (ToolV1.isXml(res.data)) {
				let video = ToolV1.videoList(res)
				return video.videos
			}
			let data = this.handleResponse(res)
			return data.videos || []
		} catch (err) {
			throw err
		}
	},
	// 获取资源详情
	async detail(site, id) {

		const url = `${site.api}?ac=videolist&ids=${id}`
		try {
			const res = await ajax.post(url)
			// console.log(res)
			if (ToolV1.isXml(res.data)) {
				let video = ToolV1.videoDetail(res)
				return video
			}
			let data = this.handleResponse(res)
			if (data.videos.length == 0) {
				return null
			}
			const model = data.videos[0]
			let video = {
				data: { ...model },
				m3u8List: []
			}
			if (!model.vod_play_url) {
				return video
			}
			let urls = model.vod_play_url.split('#')
			video.m3u8List = urls
			// console.log(video)
			return video
		} catch (err) {
			return err
		}
	},
	// 通过 json url 导入视频源
	async site(jsonUrl) {
		try {
			const res = await ajax.get(jsonUrl)
			return res.data
		} catch (err) {
			return err
		}
	}
}

export default http